package com.sun.stringescapeutils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RestController
@RequestMapping("/mail")
@Slf4j
public class AttachmentsMailTemp {
    @Autowired
    private JavaMailSender javaMailSender;

    @RequestMapping("/attTemp")
    public void sendMail() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom("thesunxueteng@163.com");
        mimeMessageHelper.setTo("1791540641@qq.com");

        mimeMessageHelper.setSubject("主题：发送有附件的邮件");
        mimeMessageHelper.setText("您好，我是小黄，我正在测试发送一封有附件的邮件。");

        FileSystemResource fileSystemResource1 = new FileSystemResource(
                new File("C:\\Users\\SUN\\Pictures\\" +
                        "Saved Pictures\\微信图片_20200518210809.jpg"));
        FileSystemResource fileSystemResource2 = new FileSystemResource(
                new File("F:\\postgraduate\\ana_start\\" +
                        "孙空气处理机组\\第二篇小论文\\CNN_LGBM_AHU_FDD_v5 - 副本.docx"));

        mimeMessageHelper.addAttachment("回忆.jpg",fileSystemResource1);
        mimeMessageHelper.addAttachment("CNN_LGBM_AHU_FDD_v5 - 副本.docx",fileSystemResource2);
        try{
            javaMailSender.send(mimeMessage);
            log.info("小黄的测试待附件的邮件已经发送。");
        }catch (Exception e){
            log.error("小黄发送带附件邮件时2发生异常了："+e);
        }
    }
}
